package com.sgb.kjwl.view.ui.adapter.self;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.self.PermissionMenuEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;


/*
  个人中心的权限菜单的第二层RecycleView适配器
 */
public class SelfSecondOrderNestingAdapter extends BaseRecycleAdapter<PermissionMenuEntity.ListEntity> {
    //设置全局点击的LinearLayout
    private LinearLayout layout_itme;
    //图标
    private ImageView iv_icon;
    //本地图片地址ID
    private int resID;
    private TextView tv_little_red_circle;

    // 绑定数据和参数
    public SelfSecondOrderNestingAdapter(Context context, List<PermissionMenuEntity.ListEntity> datas) {
        super(context, R.layout.adapter_self_secondordernestingrecyclerview_item, datas);
    }

    //实现具体操作二级菜单图片绑定
    @Override
    public void convert(ItemViewHolder holder, PermissionMenuEntity.ListEntity entity) {
        //DataBinding绑定布局，实现数据传递
        holder.setBinding(BR.viewModel, entity);
        //全局LinearLayout
        layout_itme = holder.itemView.findViewById(R.id.layout_itme);
        //图标
        iv_icon = holder.itemView.findViewById(R.id.iv_icon);
        //未读数
        tv_little_red_circle = holder.itemView.findViewById(R.id.tv_little_red_circle);
        //获取entity的图标信息
        String icon = entity.getIcon();
        if ("4_wodebaojia".equals(entity.getName())) {
            icon = "wodebaojia_4";
        }

        if ("dizhiguanli".equals(entity.getIcon()) && ("18_dizhiguanli".equals(entity.getName()) || "0_dizhiguanli".equals(entity.getName()))){
            icon = "dizhiguanli_2";
        }

        if ("0_shangpincuxiao".equals(entity.getName()) && "shangpincuxiao".equals(entity.getIcon())) {
            icon = "shangpincuxiao_2";
        }

        if ("2_wodebaojia".equals(entity.getName())) {
            icon = "wodebaojia_2";
        }

        if ("8_wodebaojia".equals(entity.getName())) {
            icon = "wodebaojia_8";
        }

        if ("0_tuanduixinxi".equals(entity.getName())) {
            icon = "jibenxinxi";
        }

        if ("7_jingjiafabu".equals(entity.getName()) && "jingjiafabu".equals(entity.getIcon())) {
            icon = "jingjiafabuwuliu";
        }

        if ("15_zhishichanquan".equals(entity.getName())) {
            icon = "zhishichanquan_15";
        }
        if ("5_hanxun".equals(entity.getName())){
            icon = "hanxun";
        }
//        if ("1_daiban".equals(entity.getName())){
//            icon = "daibanshixiang";
//        }
        if ("10_xunjia".equals(entity.getName()) || "xunjia".equals(entity.getIcon())){
            icon = "xunjia_2";
        }
        if ("12_ucjingjia".equals(entity.getName()) || "ucjingjia".equals(entity.getIcon())){
            icon = "jingjia_2";
        }
        if ("15_qiyezhuanxiang".equals(entity.getName()) || "qiyezhuanxiang1".equals(entity.getIcon())){
            icon = "qiyezhuanxiang_2";
        }

//        if ("20_wodexunjia".equals(entity.getName()) || "19_wodexunjia".equals(entity.getIcon())){
//            icon = "wodexunjia_logistics";
//        }
//        if ("21_wodebaojia".equals(entity.getName())){
//            icon = "wodebaojia_logistics";
//        }

        if (entity.getName().equals("1_daibanshixiang") || entity.getName().equals("2_wofaqi") || entity.getName().equals("3_yishenpi") || entity.getName().equals("4_chaosongwo")
          || entity.getName().equals("5_hanxun") || entity.getName().equals("7_dingdan") || entity.getName().equals("8_fukuan")
                || entity.getName().equals("11_ucshenqing") || entity.getName().equals("1_daiban")
               || entity.getName().equals("24_xinyuangong") || entity.getName().equals("24_xinhuiyuan") || entity.getName().equals("24_xinchengyuan")
              || entity.getName().equals("10_xunjia") || entity.getName().equals("13_ucjicai")) {
            tv_little_red_circle.setVisibility(entity.getCount() == 0 ? View.GONE : View.VISIBLE);
            if (entity.getCount() >= 10) {
                tv_little_red_circle.setBackgroundResource(R.drawable.shape_little_red_long_circle);
            } else {
                tv_little_red_circle.setBackgroundResource(R.drawable.icon_red_tips);
            }

            tv_little_red_circle.setText(String.format("%s", entity.getCount() >= 100
                    ? String.format("%s+", 99)
                    : entity.getCount() + ""));
        } else {
            tv_little_red_circle.setVisibility(View.GONE);
        }

        //判断是否存在
        if (!TextUtils.isEmpty(icon)) {
            //log打印
//            Log.i("api123" + entity.getTitle() + "    :    ", entity.getId());
            //获取到对应的本地图标地址id
            resID = context.getResources().getIdentifier(icon, "drawable", context.getPackageName());
            Log.e("图标名称", icon);
        }
        //判断本地图标地址id是否找到
        if (resID != 0) {
            //Glide加载本地的图标图片 .error( R.drawable.error) //异常时候显示的图片
            //    .placeholder( R.drawable.default) //加载成功前显示的图片
            //    .fallback( R.drawable.blank) //url为空的时候,显示的图片
            Glide.with(context)
                    .load(resID)
                    .apply(new RequestOptions()
                            .error(R.drawable.user_center_def_img)
                            .placeholder(R.drawable.user_center_def_img)
                            .fallback(R.drawable.user_center_def_img))
                    .into(iv_icon);
        } else {
            //Glide加载本地的默认图标图片
//            Glide.with(context).load(R.drawable.menu_bg).into(iv_icon);
            Glide.with(context).load(R.drawable.user_center_def_img).into(iv_icon);
        }
    }
}
